Fix up locale strings for xm
authorTom Wilkie <tom.wilkie@gmail.com>
Fri, 13 Apr 2007 15:46:41 +0000 (16:46 +0100)
committerTom Wilkie <tom.wilkie@gmail.com>
Fri, 13 Apr 2007 15:46:41 +0000 (16:46 +0100)
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
config/StdGNU.mk
tools/python/Makefile
tools/python/xen/xm/XenAPI.py

index fd7bc53a56edc3c3975690c18e2a4da95565faf3..4f08ea9a3088473122c12d37f8aee0c20eea46f4 100644 (file)
@@ -10,6 +10,7 @@ OBJCOPY    = $(CROSS_COMPILE)objcopy
 OBJDUMP    = $(CROSS_COMPILE)objdump
 
 MSGFMT     = msgfmt
+MSGMERGE   = msgmerge
 
 INSTALL      = install
 INSTALL_DIR  = $(INSTALL) -d -m0755 -p
index ae4254c6e68810c67566c76bbef1942d61055d47..309c815352c71ea362b3d7ce2baa5a58b034a5a3 100644 (file)
@@ -4,17 +4,54 @@ include $(XEN_ROOT)/tools/Rules.mk
 .PHONY: all
 all: build
 
-.PHONY: build
-build:
+# For each new supported translation, add its name here, eg 'fr_FR'
+# to cause the .po file to be built & installed, eg
+LINGUAS :=
+POPACKAGE := xen-xm
+PODIR := xen/xm/messages
+POTFILE := $(PODIR)/xen-xm.pot
+I18NSRCFILES = $(shell find xen/xm/ -name '*.py')
+CATALOGS = $(patsubst %,xen/xm/messages/%.mo,$(LINGUAS))
+NLSDIR = /usr/share/locale
+
+.PHONY: build buildpy
+buildpy:
        CC="$(CC)" CFLAGS="$(CFLAGS)" python setup.py build
-       if which $(MSGFMT) >/dev/null ; then \
-          for file in `cd ./xen/xm; find messages -name xen-xm.po`; do \
-            dest=`echo "build/$$file" | \
-                  sed -e 's#xen-xm.po#LC_MESSAGES/xen-xm.mo#'`; \
-            mkdir -p `dirname "$$dest"`; \
-            $(MSGFMT) -c -o "$$dest" "xen/xm/$$file"; \
-          done; \
-        fi
+
+build: buildpy refresh-pot refresh-po $(CATALOGS)
+
+# NB we take care to only update the .pot file it strings have
+# actually changed. This is complicated by the embedded date
+# string, hence the sed black magic. This avoids the expensive
+# re-generation of .po files on every single build
+refresh-pot: $(I18NSRCFILES)
+       xgettext --default-domain=$(POPACAKGE) \
+               --keyword=N_ \
+               --keyword=_ \
+               -o $(POTFILE)-tmp \
+               $(I18NSRCFILES)
+       sed -f remove-potcdate.sed < $(POTFILE) > $(POTFILE)-1
+       sed -f remove-potcdate.sed < $(POTFILE)-tmp > $(POTFILE)-2
+       if cmp -s $(POTFILE)-1 $(POTFILE)-2; then \
+               rm -f $(POTFILE)-tmp $(POTFILE)-1 $(POTFILE)-2; \
+       else \
+               mv $(POTFILE)-tmp $(POTFILE); \
+                rm -f $(POTFILE)-1 $(POTFILE)-2; \
+       fi
+
+refresh-po: $(POTFILE)
+       for l in $(LINGUAS); do \
+               if $(MSGMERGE) $(PODIR)/$$l.po $(POTFILE) > $(PODIR)/$$l-tmp ; then \
+                       mv -f $(PODIR)/$$l-tmp $(PODIR)/$$l.po ; \
+                       echo "$(MSGMERGE) of $$l.po succeeded" ; \
+               else \
+                       echo "$(MSGMERGE) of $$l.po failed" ; \
+                       rm -f $(PODIR)/$$l-tmp ; \
+               fi \
+       done
+
+%.mo: %.po
+       $(MSGFMT) -c -o $@ $<
 
 .PHONY: install
 ifndef XEN_PYTHON_NATIVE_INSTALL
@@ -28,8 +65,13 @@ endif
 
 install-messages: all
        if which $(MSGFMT) >/dev/null ; then \
-         mkdir -p "$(DESTDIR)/usr/share/locale"; \
-         cp -R build/messages/* "$(DESTDIR)/usr/share/locale/"; \
+               mkdir -p $(DESTDIR)$(NLSDIR); \
+               for l in $(LINGUAS); do \
+                       $(INSTALL) -m 755 -d $(DESTDIR)$(NLSDIR)/$$l; \
+                       $(INSTALL) -m 755 -d $(DESTDIR)$(NLSDIR)/$$l/LC_MESSAGES; \
+                       $(INSTALL) -m 644 $(PODIR)/$$l.mo \
+                               $(DESTDIR)$(NLSDIR)/$$l/LC_MESSAGES/$(POPACKAGE).mo; \
+               done ; \
        fi
 
 .PHONY: test
@@ -38,4 +80,4 @@ test:
 
 .PHONY: clean
 clean:
-       rm -rf build *.pyc *.pyo *.o *.a *~
+       rm -rf build *.pyc *.pyo *.o *.a *~ $(CATALOGS)
index 74be43cbb341bbdc31a742a381428f0617cfa165..a3717d70f24716db6d12b9cf3603b6ce0b06e656 100644 (file)
@@ -49,6 +49,22 @@ import xmlrpclib
 
 import xen.util.xmlrpcclient as xmlrpcclient
 
+def gettext_noop(str):
+    return str
+
+N_ = gettext_noop
+
+errormap = {
+    "INTERNAL_ERROR": N_("Internal error: %(1)s."),
+    "MAP_DUPLICATE_KEY": N_("This map already contains %(1)s -> %(2)s."),
+    "MESSAGE_METHOD_UNKNOWN": N_("The method %(1)s is unsupported."),
+    "MESSAGE_PARAMETER_COUNT_MISMATCH": N_("The method %(1)s takes %(2)s argument(s) (%(3)s given)."),
+    "SESSION_AUTHENTICATION_FAILED": N_("Permission denied."),
+    "VALUE_NOT_SUPPORTED": N_("Value \"%(2)s\" for %(1)s is not supported by this server.  The server said \"%(3)s\"."),
+    "HANDLE_INVALID": N_("The %(1)s handle %(2)s is invalid."),
+    "OPERATION_NOT_ALLOWED": N_("You attempted an operation that was not allowed."),
+    "NETWORK_ALREADY_CONNECTED": N_("The network you specified already has a PIF attached to it, and so another one may not be attached."),
+    }
 
 translation = gettext.translation('xen-xm', fallback = True)
 
@@ -68,7 +84,7 @@ class Failure(Exception):
 
     def __str__(self):
         try:
-            return translation.ugettext(self.details[0]) % self._details_map()
+            return translation.ugettext(errormap[self.details[0]]) % self._details_map()
         except TypeError, exn:
             return "Message database broken: %s.\nXen-API failure: %s" % \
                    (exn, str(self.details))